<?php

namespace application\Module;

use application\Comm\Func;
use library\Core\Db\CDbCriteria;
use library\Core\Config;
use application\Comm\Request;
use application\Model\ShowHomeModel;

class ShowHome
{
    /**
     * @param 
     * @param array $select
     * @return array
     * 获取晒家列表
     */
    public function getList($select = [],$limit = 10)
    {
    	$category_id = Config::getParamsVal('PRODUCT_CATEGORY');
    	$style_id = Config::getParamsVal('STYLE');
        $criteria = new CDbCriteria();
        //分页
        $page['now_page'] = Request::getParam('p') ? Request::getParam('p') : 1;
        $page['list_rows'] = Request::getParam('list_rows') ? Request::getParam('list_rows') : $limit;
        $offset = ($page['now_page'] - 1) * $page['list_rows'];
        
        $criteria->select($select);
        $criteria->addEqCondition('is_del', 0);
        $criteria->addEqCondition('status', 1);
        $criteria->addOrderDesc('orderby');
        $ShowHomeModel = new ShowHomeModel();
        
        $page['total_rows'] = $ShowHomeModel->getCount($criteria);
        $criteria->setOffset($offset);
        $criteria->setLimit($page['list_rows']);
        
        $data = $ShowHomeModel->getListForArray($criteria);
    	foreach ($data as $key => &$val){
    		$val['description'] = mb_substr($val['description'],0,100,'utf-8').'……';
    		$val['category_id'] = $category_id[$val['category_id']] ? $category_id[$val['category_id']] : '';
    		$val['style_id'] = $style_id[$val['style_id']] ? $style_id[$val['style_id']] : '';
    		$val['acreage'] = $val['acreage'] ? $val['acreage'].'㎡' : '';
    		$val['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
    		$val['thumb'] = $val['thumb'] ? Func::imgUrl($val['thumb']) : '';
    		$val['src'] = $val['src'] ? Func::imgUrl($val['src']) : '';
    	}
        return ['data' => $data,'page' => $page];
    }
    
    /**
     * @param 
     * @param array $select
     * @return array
     * 根据热门排序获取热门晒家数据
     */
    public function getHot($type = 0,$select = ['id','title','src'],$limit = 4){
    	
    	$criteria = new CDbCriteria();
    	$criteria->select($select);
    	$criteria->addEqCondition('is_del', 0);
    	$criteria->addEqCondition('status', 1);
    	if ($type == 0){
    		$criteria->addOrderDesc('hot_order');
    	}else{
    		$criteria->addOrderDesc('create_time');
    	}
    	$criteria->setLimit($limit);
    	$ShowHomeModel = new ShowHomeModel();
    	$data = $ShowHomeModel->getListForArray($criteria);
    	foreach ($data as $key => &$val){
    		$val['src'] = $val['src'] ? Func::imgUrl($val['src']) : '';
    	}
    	return $data;
    }
    
    /**
     * @param $id
     * @param array $select
     * @return array
     * 根据id获取热门晒家数据
     */
    public function getView($id,$select = ['id','user_name','category_id','style_id','acreage','title','description','thumb','src','contents','create_time']){
    	$category_id = Config::getParamsVal('PRODUCT_CATEGORY');
    	$style_id = Config::getParamsVal('STYLE');
    	$criteria = new CDbCriteria();
    	$criteria->select($select);
    	$criteria->addEqCondition('id', $id);
    	$criteria->addEqCondition ( 'is_del', 0 );
		$criteria->addEqCondition ( 'status', 1 );
		
		$ShowHomeModel = new ShowHomeModel ();
		$data = $ShowHomeModel->getRow ( $criteria );
		
		$data ['description'] = mb_substr ( $data ['description'], 0, 100, 'utf-8' ) . '……';
		$data ['category_id'] = $category_id [$data ['category_id']] ? $category_id [$data ['category_id']] : '';
		$data ['style_id'] = $style_id [$data ['style_id']] ? $style_id [$data ['style_id']] : '';
		$data ['acreage'] = $data ['acreage'] ? $data ['acreage'] . '㎡' : '';
		$data ['create_time'] = date ( 'Y-m-d H:i:s', $data ['create_time'] );
		$data ['thumb'] = $data['thumb'] ? Func::imgUrl($data['thumb']) : '';
    	$data['src'] = $data['src'] ? Func::imgUrl($data['src']) : '';
    	
    	return $data;
    }
    
}
